define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Fast.config.openArea = ['95%','95%'];
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/course/index' + location.search,
                    add_url: 'course/course/add',
                    edit_url: 'course/course/edit',
                    del_url: 'course/course/del',
                    multi_url: 'course/course/multi',
                    import_url: 'course/course/import',
                    table: 'course',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='tag_ids']", form).addClass("selectpage").data("source", "course/tags/selectpage")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("multiple", true)
                    .data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'category_id', title: __('Category'), operate: 'FIND_IN_SET', searchList: Config.categoryGroup, formatter: Table.api.formatter.label},

                        // {field: 'category.name', title: __('Category'), operate: 'LIKE'},
                        {field: 'tag_ids', title: __('Tags'), formatter: Table.api.formatter.search,visible: false},

                        {field: 'tags', title: __('Tags'), operate: false, formatter: Table.api.formatter.flag},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'class_hour', title: __('Class_hour')},
                        {field: 'is_recommend', title: __('Is_recommend'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        select_classes: function () {
            var classes_res =  [];
            $('.time_checkbox').change(function() {
                var school_id = $(this).attr('data-school_id');
                var school_name = $(this).attr('data-school_name');
                var classes_id = $(this).attr('data-classes_id');
                var classes_name = $(this).attr('data-classes_name');
                var w = $(this).attr('data-w');

                var time_text = $(this).attr('data-w_name') + " " + $(this).attr('data-time_text');
                var time_no = $(this).attr('data-time_no');
                var select_count = $(this).attr('data-select_count');
                if($(this).is(":checked")) {
                    if(select_count  > 0){
                        $.each(classes_res,function(index,items){
                            //不同的班次 需要重新选择
                           if(items.school_id != school_id || items.classes_id != classes_id){
                               $(".time_checkbox").prop('checked',false);
                               classes_res = [];
                           }else {
                               //相同的班次 只能
                           }
                        });
                        if(classes_res.length >= select_count){
                            $(".time_checkbox").prop('checked',false);
                            classes_res = [];
                        }

                        classes_res.push({
                            "school_id":school_id,
                            "school_name":school_name,
                            "classes_id":classes_id,
                            "classes_name":classes_name,
                            "w":w,
                            "time_text":time_text,
                            "time_no":time_no,
                            "select_count":select_count,

                        });
                    }
                    $(this).prop('checked',true);




                    // var returnVal = confirm("Are you sure?");
                    // $(this).attr("checked", returnVal);
                }else {
                    $.each(classes_res,function(index,items){
                        // console.log(items);
                        //取消选择的话 把已选的 删除掉
                        if(items.school_id == school_id && items.classes_id == classes_id && items.w == w && items.time_no == time_no){
                            classes_res.splice(index,1);
                            return false;
                        }
                    });
                }
                console.log(classes_res);
            });
            //点击搜索按钮
            var close = function (data) {
                var index = parent.Layer.getFrameIndex(window.name);
                var callback = parent.$("#layui-layer" + index).data("callback");
                //再执行关闭
                parent.Layer.close(index);
                //再调用回传函数
                if (typeof callback === 'function') {
                    callback.call(undefined, data);
                }
            };
            $(document).on('click', '.confirm', function () {
                var select_count = 0;
                if(classes_res.length <=0){
                    layer.msg("请选择班次");
                    return ;
                }
                $.each(classes_res,function(index,items){
                    select_count = items.select_count;
                    return false;
                });
                if(select_count != classes_res.length){
                    layer.msg("当前班次请选择"+select_count+"个班次");
                    return ;
                }

                close(classes_res);
            });
        },
        api: {
            bindevent: function () {
                $(".school_checkbox").on("click", function () {
                    var school_id = $(this).attr('data-id');
                    var school_div_class = ".school_"+school_id;
                    // console.log(school_id);
                    if ($(this).prop("checked")) {
                        $(school_div_class).removeClass("hidden");
                    }else {
                        $(school_div_class).addClass("hidden");
                    }
                });

                $(".classes_checkbox").on("click", function () {
                    var school_classes_id = $(this).attr('data-id');
                    var school_classes_div_class = ".classes_"+school_classes_id;
                    // console.log(school_id);
                    if ($(this).prop("checked")) {
                        $(school_classes_div_class).removeClass("hidden");
                    }else {
                        $(school_classes_div_class).addClass("hidden");
                    }
                });

                $(".time_checkbox").on("click", function () {
                    var school_classes_time_id = $(this).attr('data-id');
                    var school_classes_time_div_class = ".classes_"+school_classes_time_id;
                    // console.log(school_id);
                    if ($(this).prop("checked")) {
                        $(school_classes_time_div_class).removeClass("hidden");
                    }else {
                        $(school_classes_time_div_class).addClass("hidden");
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
